/*
 * JSF Toolkit Component Framework
 * Copyright (C) 2007 Noah Sloan <iamnoah A-T gmail D0T com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 */
package com.jsftoolkit.base.components;

import static com.jsftoolkit.gen.GenerateUtils.defaultStream;

import java.io.PrintStream;

import com.jsftoolkit.gen.GenerateUtils;
import com.jsftoolkit.gen.info.ComponentInfo;
import com.jsftoolkit.gen.writer.ClassWriter;
import com.jsftoolkit.gen.writer.ClassWriterFactory;
import com.jsftoolkit.gen.writer.impl.BasicClassWriter;
import com.jsftoolkit.utils.Utils;

public class GenerateComponents {

	private static final class GplWriter extends BasicClassWriter {

		public GplWriter(PrintStream out) {
			super(out);
		}

		@Override
		public void printFileComment() {
			print(HEADER);
		}
	}

	private static final String HEADER = Utils.resourceToString("/HEADER.txt",
			"UTF-8", "");

	/**
	 * Generates the base components.
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// only need a tag handler for pass through
		{
			ComponentInfo info = new PassThroughTagSpec().getInfo();
			GenerateUtils.generateTag(info, new GplWriter(defaultStream(info.getTag())));
		}

		// data iterator needs everything
		{
			final ComponentInfo info = new DataIteratorSpec().getInfo();
			GenerateUtils.genAndUpdateAll(new ClassWriterFactory() {
				public ClassWriter newInstance(PrintStream out) {
					return new GplWriter(out);
				}
			}, info);
		}

		// these are self rendered
		ComponentInfo[] noRenderer = { new HtmlScriptsSpec().getInfo(),
				new IncludeResourceSpec().getInfo() };
		for (ComponentInfo info : noRenderer) {
			GenerateUtils.generateTag(info, new GplWriter(defaultStream(info.getTag())));
			GenerateUtils.generateComponent(info, new GplWriter(defaultStream(info)));
			GenerateUtils.updateAll(info);
		}
	}
}
